class Solution {
public:
    int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>>f(n, vector<int>(m, 0x3f3f3f3f));
        for (int i = 0; i < m; i++) {
            f[0][i] = grid[0][i];
        }
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < m; k++) {
                    f[i][j] = min(f[i][j], f[i - 1][k] + moveCost[grid[i - 1][k]][j] + grid[i][j]);
                }
            }
        }
        int ans = 1e9;
        for (int i = 0; i < m; i++) {
            ans = min(ans, f[n - 1][i]);
        }
        return ans;
    }
};